#include <boost/foreach.hpp>

#include "tinyxml.h"
#include "stdafx.h"
#include "collada.h"

#ifdef _DEBUG
#define DEBUG_NEW new( _NORMAL_BLOCK, __FILE__, __LINE__ )
#define new DEBUG_NEW
#endif

using namespace Collada;

IMPLEMENT_RTTI( Collada::Geometry, Collada::Element )
IMPLEMENT_ELEMENT( Collada::Geometry )

Geometry::Geometry( const std::string& id, const std::string& name )
:	Element( id, name )
{
	CHILD_ELEMENT_BEGIN()
		CHILD_ELEMENT( "asset", Collada::Empty )
		CHILD_ELEMENT( "convex_mesh", Collada::Empty )
		CHILD_ELEMENT( "mesh", Collada::Mesh )
		CHILD_ELEMENT( "spline", Collada::Empty )
		CHILD_ELEMENT( "brep", Collada::Empty )
	CHILD_ELEMENT_END()
}

Geometry::~Geometry()
{
	std::vector< Element* >::iterator it = _datas.begin();
	for( ; it != _datas.end(); ++it )
	{
		if( ( *it ) )
		{
			delete ( *it );
		}
	}
	//_datas.clear();
}

bool Geometry::AddGeometricData( Element* pData )
{
	_datas.push_back( pData );
	return true;
}

void Geometry::AllChildElementsLoaded( Element* pParent )
{
	( ( Library* )pParent )->AddGeometry( this );
}

void Geometry::Initialize( Element* pParent, const void* desc )
{
}

bool Geometry::IsValid( const void* desc )
{
	TiXmlElement* pChild = ( ( TiXmlElement* )desc )->FirstChildElement();
	if( std::string( pChild->Value() ) == "asset" )
	{
		pChild = pChild->NextSiblingElement();
	}

	std::string elementName = pChild->Value();
	if( elementName == "convex_mesh" ||
		elementName == "mesh" ||
		elementName == "spline" ||
		elementName == "brep" )
	{
		return true;
	}

	return false;
}
